package com.tiandao.mofang.core.shilian;

import com.tiandao.mofang.core.RobotSingleton;
import com.tiandao.mofang.core.role.BaseDG;
import com.tiandao.mofang.ui.MFFrame;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

/**
 * 兵器谱自动化
 */
public class BingQiPu implements Runnable{
    private static Robot robot = RobotSingleton.getInstance();
    @Override
    public void run() {
        start();
    }

    public static void start(){
        open();
        MFFrame.textArea.append(new Date()+":    进入兵器谱位面!\n");
        System.out.println(" 进入兵器谱位面!");
        duiHua();
        MFFrame.textArea.append(new Date()+":    第一层结束!\n");
        System.out.println(" 第一层结束");
        orther();
        MFFrame.textArea.append(new Date()+":    第二层结束!\n");
        System.out.println("  第二层结束!");
        orther();
        MFFrame.textArea.append(new Date()+":    第三层结束!\n");
        System.out.println("  第三层结束!");
        orther();
        MFFrame.textArea.append(new Date()+":    第四层结束!\n");
        System.out.println("  第四层结束!");
        orther();
        MFFrame.textArea.append(new Date()+":    第五层结束!\n");
        System.out.println("  第五层结束!");
        orther();
        MFFrame.textArea.append(new Date()+":    第六层结束!\n");
        System.out.println("  第六层结束!");
        orther();
        MFFrame.textArea.append(new Date()+":    第七层结束!\n");
        System.out.println("  第七层结束!");
        orther();
        MFFrame.textArea.append(new Date()+":    第八层结束!\n");
        System.out.println("  第八层结束!");
        orther();
        MFFrame.textArea.append(new Date()+":    第九层结束!\n");
        System.out.println("  第九层结束!");
        orther();
        MFFrame.textArea.append(new Date()+":    第十层结束!\n");
        System.out.println("  第十层结束!");
        //清空线程
//        MFFrame.t = null;
//        MFFrame.tKey = 1;
    }
    //开启兵器谱的方法
    public static void open(){
        try {
            //等待五秒
            Thread.sleep(5000);
            //移动到综合上等待1.5秒 并点击
            robot.mouseMove(1710,1030);
            Thread.sleep(1500);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            //移动到兵器谱上等待1.5秒 并点击
            Thread.sleep(1500);
            robot.mouseMove(1710,1000);
            Thread.sleep(1500);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            //移动到开始挑战上等待1.5秒 并点击
            Thread.sleep(2000);
            robot.mouseMove(630,820);
            Thread.sleep(1500);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            //移动到确定上等待1.5秒 并点击
            Thread.sleep(2000);
            robot.mouseMove(885,410);
            Thread.sleep(1500);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            //读条等待
            Thread.sleep(18000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //位面对话开启第一层
    public static void duiHua(){
        try {
            //ESC关掉界面
            Thread.sleep(2000);
            robot.keyPress(KeyEvent.VK_ESCAPE);
            robot.keyRelease(KeyEvent.VK_ESCAPE);
            Thread.sleep(2000);
            //点击"开启挑战"去对话
            robot.mouseMove(1820,410);
            Thread.sleep(2000);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            //等待走路,并对话开启
            Thread.sleep(12000);
            robot.mouseMove(288,550);
            Thread.sleep(2000);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            //等待八秒开启第一层
            Thread.sleep(8000);
            robot.mouseMove(670,660);
            Thread.sleep(2000);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            //打怪
            Thread.sleep(3500);
            //开启打怪线程
            Thread t = new Thread(new BaseDG());
            t.start();
            //开启监听线程
            while (true){
                Thread.sleep(200);
                if(ColorFx()){
                    //打完怪了 停止打怪线程翻牌
                    t.stop();
                    //翻牌
                    fanPai();
                    break;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void orther(){
        //等待八秒开启第一层
        try {
            Thread.sleep(13000);
            robot.mouseMove(670,660);
            Thread.sleep(2000);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            //打怪
            Thread.sleep(3500);
            //开启打怪线程
            Thread t = new Thread(new BaseDG());
            t.start();
            //开启监听线程
            while (true){
                Thread.sleep(100);
                if(ColorFx()){
                    //打完怪了 停止打怪线程翻牌
                    t.stop();
                    //翻牌
                    fanPai();
                    break;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //翻牌
    public static void fanPai(){
        try {
            Thread.sleep(18000);
            Random random = new Random();
            int a = random.nextInt(3);
            if(a==0){
                robot.mouseMove(700,540);
                Thread.sleep(2000);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            }
            if(a==1){
                robot.mouseMove(900,540);
                Thread.sleep(2000);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            }
            if(a==2){
                robot.mouseMove(1090,540);
                Thread.sleep(2000);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //测试
    public static void main(String[] args) {
//        open();
//        robot.mouseMove(885,410);
//        robot.mouseMove(1820,410);
//        duiHua();
//        robot.mouseMove(288,550);
//        robot.mouseMove(700,540);//一
//        robot.mouseMove(900,540);
//        robot.mouseMove(1090,540);
        start();
    }


    public static Boolean ColorFx(){
//        Long starttime = System.currentTimeMillis();
            //获取屏幕截图
            //主分线问题
//        BufferedImage img = robot.createScreenCapture(new Rectangle(1295, 4, 50, 25));
            BufferedImage img = robot.createScreenCapture(new Rectangle(840, 245, 230, 15));
            //获取屏幕截图的RGB数组
//        try {
//            ImageIO.write(img, "png", new File("D:/jina/bqp.png"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
            int[] rgbS = img.getRGB(0,0,230,15,null,0,230);
            int r = 0;
            int g = 0;
            int b = 0;
            //红色系计数
            int weiNum = 0;
            //循环判断所有像素点
            for(int i = 0;i<rgbS.length;i++){
                Color myColor = new Color(rgbS[i]);
                r = myColor.getRed();
                g = myColor.getGreen();
                b = myColor.getBlue();
                if(r>=247&&r<=254&&g>=220&&g<=228&&b>=142&&b<=149){
                    weiNum++;
                }
                if(weiNum>10){
                    break;
                }
            }
            if(weiNum>10){
                return true;
            }
            return false;
        }
}
